package cn.school.demo.ui.modify_password

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.navigation.fragment.findNavController
import cn.school.demo.MyApplication
import cn.school.demo.R
import cn.school.demo.databinding.FragmentModifyPwdBinding
import cn.school.demo.util.StringUtil
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.StringUtils
import com.mylhyl.circledialog.CircleDialog

class ModifyPwdFragment : Fragment() {

    private lateinit var viewModel: ModifyPwdViewModel
    private lateinit var binding: FragmentModifyPwdBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        viewModel = ViewModelProvider(this).get(ModifyPwdViewModel::class.java)
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_modify_pwd, container, false)
        binding.vw = this
        binding.vm = viewModel
        binding.lifecycleOwner = viewLifecycleOwner
        return binding.root
    }

    fun gotoSetting() = MyApplication.goto(
        R.id.action_ModifyPwd_to_Setting,
        findNavController(),
        requireFragmentManager()
    )

    fun seePwd() {
        viewModel.changeBrowse()
        if (viewModel.isBrowse.value!!) {
            LogUtils.d("我睁眼了！！")
            binding.modifyBrowse.setImageResource(R.drawable.browse)
            binding.editTextTextPassword.transformationMethod =
                HideReturnsTransformationMethod.getInstance()
        } else {
            LogUtils.d("我闭眼了！！")
            binding.modifyBrowse.setImageResource(R.drawable.unbrowse)
            binding.editTextTextPassword.transformationMethod =
                PasswordTransformationMethod.getInstance()
        }

    }

    fun editTextTextChange() {
        viewModel.changeEdit(binding.editTextTextPassword.text.toString())
        binding.modifyButton.isEnabled = viewModel.pwd.value?.length != 0
    }

    fun gotoNext() {
        when {
            StringUtil.isPassword(viewModel.pwd.value.toString()) &&
                    MyApplication.user!!.password == viewModel.pwd.value.toString()
            -> MyApplication.goto(
                R.id.action_ModifyPwd_to_ModifyPwdSecond,
                findNavController(),
                requireFragmentManager()
            )
            !StringUtil.isPassword(viewModel.pwd.value.toString()) -> CircleDialog.Builder()
                .setText("密码不合法！").setNegative("确定", null)
                .show(requireFragmentManager())
            else -> CircleDialog.Builder()
                .setText("密码错误！").setNegative("确定", null)
                .show(requireFragmentManager())
        }

    }

}